package cn.iocoder.an.module.mold.controller.admin.recheckcategory;

import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryImportExcelVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepcategory.vo.UpkeepCategoryRespVO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcategory.UpkeepCategoryDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.recheckcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckcategory.RecheckCategoryDO;
import cn.iocoder.an.module.mold.service.recheckcategory.RecheckCategoryService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 复检类型")
@RestController
@RequestMapping("/mold/recheck-category")
@Validated
public class RecheckCategoryController {

    @Resource
    private RecheckCategoryService recheckCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建复检类型")
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:create')")
    public CommonResult<Long> createRecheckCategory(@Valid @RequestBody RecheckCategorySaveReqVO createReqVO) {
        return success(recheckCategoryService.createRecheckCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新复检类型")
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:update')")
    public CommonResult<Boolean> updateRecheckCategory(@Valid @RequestBody RecheckCategorySaveReqVO updateReqVO) {
        recheckCategoryService.updateRecheckCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除复检类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:delete')")
    public CommonResult<Boolean> deleteRecheckCategory(@RequestParam("id") Long id) {
        recheckCategoryService.deleteRecheckCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除复检类型")
                @PreAuthorize("@ss.hasPermission('mold:recheck-category:delete')")
    public CommonResult<Boolean> deleteRecheckCategoryList(@RequestParam("ids") List<Long> ids) {
        recheckCategoryService.deleteRecheckCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得复检类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:query')")
    public CommonResult<RecheckCategoryRespVO> getRecheckCategory(@RequestParam("id") Long id) {
        RecheckCategoryDO recheckCategory = recheckCategoryService.getRecheckCategory(id);
        return success(BeanUtils.toBean(recheckCategory, RecheckCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得复检类型分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:query')")
    public CommonResult<PageResult<RecheckCategoryRespVO>> getRecheckCategoryPage(@Valid RecheckCategoryPageReqVO pageReqVO) {
        PageResult<RecheckCategoryDO> pageResult = recheckCategoryService.getRecheckCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckCategoryRespVO.class));
    }



    @GetMapping("/all")
    @Operation(summary = "获得复检类型列表")
    public CommonResult<List<RecheckCategoryRespVO>> getRecheckCategoryList() {
        List<RecheckCategoryDO> list = recheckCategoryService.getRecheckCategoryAll();
        return success(BeanUtils.toBean(list, RecheckCategoryRespVO.class));
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出复检类型 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckCategoryExcel(@Valid RecheckCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckCategoryDO> list = recheckCategoryService.getRecheckCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "复检类型.xls", "数据", RecheckCategoryRespVO.class,
                        BeanUtils.toBean(list, RecheckCategoryRespVO.class));
    }




    @GetMapping("/get-import-template")
    @Operation(summary = "获得复检类型导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<RecheckCategoryImportExcelVO> list = Arrays.asList(
                RecheckCategoryImportExcelVO.builder()
                        .name("测试")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "复检导入模板.xlsx", "列表", RecheckCategoryImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图号")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<RecheckCategoryImportExcelVO> list = ExcelUtils.read(file, RecheckCategoryImportExcelVO.class);
        return success(recheckCategoryService.importRecheckCategoryList(list, updateSupport));
    }


}